import * as Vue from "vue";
import { scrolltoelement } from "./scrolltoelement";
import { states } from "./states";

export function scrolltoelementid(id: string) {
    const ele = document.getElementById(id);

    ele &&
        requestAnimationFrame(async () => {
            await Vue.nextTick();

            requestAnimationFrame(() => {
                if (states.firstloaded >= 1) {
                    scrolltoelement(ele);
                } else {
                    let timer = setInterval(() => {
                        //@ts-ignore
                        requestIdleCallback(() => {
                            // console.log(ele.offsetTop);
                            if (
                                Array.from(
                                    document.querySelectorAll("*")
                                ).includes(ele)
                            ) {
                                states.firstloaded++;
                                scrolltoelement(ele);
                            }
                        });
                    }, 30);

                    setTimeout(() => {
                        clearInterval(timer);
                    }, 100);
                }
            });
        });
}
